package mnstudent.businessLayer;

import mnstudent.dataacesslayer.LoginDAL;
import mnstudent.datatranferobject.LoginDTO;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class LoginBLL {

    private LoginDAL loginDAL;

    public LoginBLL() {
        loginDAL = new LoginDAL();
    }

    /**
     * Check Login
     *
     * @param username
     * @param pass
     * @return
     */
    public boolean checkLogin(String username, String pass) {
        boolean result = false;

        ResultSet rs = loginDAL.getUser(username, pass);
        try {
            if (rs.next()) {
                result = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginBLL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public void loadUser(String username, String pass) {
        ResultSet temp = loginDAL.getUser(username, pass);

        DateFormat dfm = new SimpleDateFormat("dd/MM/yyyy");
        java.util.Date d = new java.util.Date();

        try {
            while (temp.next()) {
                LoginDTO.setID(temp.getString("MaNV"));
                LoginDTO.setName(temp.getString("TenNV"));
                LoginDTO.setBirthday(temp.getDate("NgaySinh"));
                LoginDTO.setAddress(temp.getString("DiaChi"));
                LoginDTO.setPhone(temp.getString("DienThoai"));
                LoginDTO.setSex(temp.getString("GioiTinh"));
                LoginDTO.setIDPermission(temp.getString("MaCV"));
                LoginDTO.setUserName(temp.getString("TenDangNhap"));
                LoginDTO.setPass(temp.getString("MatKhau"));
                LoginDTO.setLastLogin(dfm.format(d));
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginBLL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
